import { specAdd, specTotal, specList, specOne, specUpdate, speceDel } from "../../request/api"
import { successAlert } from "../../utils/alert"
const state = {
    specs: [],
    size: 2,
    page: 1,
    count: 0
}
const mutations = {
    changeSpec(state, res) {
        state.specs = res;
    },
    changeCount(state, count) {
        state.count = count;
    },
    changePage(state, page) {
        state.page = page;
    }
}
const actions = {
    specListActions(context) {
        let params = {
            size: context.state.size,
            page: context.state.page,
        }
        specList(params).then(res => {
            if ((res.list === null || res.list.length === 0) && context.state.page > 1) {
                let page = contect.state.page - 1;
                context.commit("changePage", page);
                context.dispatch("specListActions");
                return;
            }
            if (res.code === 200) {
                context.commit("changeSpec", res.list)
            }
        })
    },
    countActions(context) {
        specTotal().then(res => {
            context.commit("changeCount", res.list[0].total);
        })
    },
    specUpdateActions(context, data) {
        specUpdate(data).then(res => {
            if (res.code === 200) {
                console.log(res);   
                successAlert(res.msg);
            }
        })
    },
    // 修改当前页码
    pageActions(context, page) {
        context.commit("changePage", page)
    }
}
const getters = {
    specs(state) {
        return state.specs;
    },
    size(state) {
        return state.size;
    },
    count(state) {
        return state.count;
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}